package com.itfreer.security;

import com.itfreer.utils.config.ConfigManager;
import com.itfreer.utils.volley.HeaderUtils;
import com.itfreer.utils.volley.RequestManager;
import com.itfreer.utils.volley.callback.DataCallback;

import java.util.Map;

/**
 * 安全数据服务，定义系统登录授权
 */
public class SecurityDataService {

    /**
     * 单一实例
     */
    public static final SecurityDataService instance = new SecurityDataService();

    private SecurityDataService() {
    }

    /**
     * 创建tgt
     * @param username
     * @param password
     * @param listener
     */
    public void createTickets(String username, String password, DataCallback<Map> listener){
        try {
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/v1/tickets?username=" + username + "&password=" + password;
            RequestManager.getInstance().post(url, HeaderUtils.getHeader(), null, listener);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 验证tgt
     * @param tgt
     * @param listener
     */
    public void ticketsValidate(String tgt, DataCallback<Map> listener){
        try {
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/ticketsValidate/" + tgt;
            RequestManager.getInstance().post(url, HeaderUtils.getHeader(), null, listener);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 创建ST
     * @param tgt
     * @param service
     * @param listener
     */
    public void createServiceTickets(String tgt, String service, DataCallback<Map> listener){
        try {
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/v1/serviceTickets/" + tgt + "?service=" + service;
            RequestManager.getInstance().post(url, HeaderUtils.getHeader(), null, listener);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 删除TGT
     * @param tgt
     * @param listener
     */
    public void deleteTickets(String tgt, DataCallback<Map> listener){
        try {
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/v1/tickets/" + tgt;
            RequestManager.getInstance().delete(url, HeaderUtils.getHeader(), null, listener);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 模拟登录，解决服务端登录问题
     * @param st
     * @param listener
     */
    public void mklogin(String st, DataCallback<Map> listener){
        try {
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/sso/mklogin/?ticket=" + st;
            RequestManager.getInstance().get(url, HeaderUtils.getHeader(), listener);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 退出登录
     * @param jwt
     * @param listener
     */
    public void loginOut(String jwt, DataCallback<Map> listener){
        try {
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/sso/logout?ticket=" + jwt;
            RequestManager.getInstance().get(url, HeaderUtils.getHeader(), listener);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 用户注册信息
     * @param ac
     * @param listener
     */
    public void userRegister(String ac, String pw, String rpw, DataCallback<Map> listener){
        try {
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/power/userRegister/" + ac + "/" + pw + "/" + rpw;
            RequestManager.getInstance().post(url, HeaderUtils.getHeader(), null, listener);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 获取用户信息，包括用户，权限
     * @param listener
     */
    public void getUserInfo(DataCallback<UserInfoEntity> listener){
        try {
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/currentuser/getUserInfo";
            RequestManager.getInstance().post(url, HeaderUtils.getHeader(), null, listener);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 获取当前用户信息
     * @param listener
     */
    public void getCurrentUser(DataCallback<UserInfoEntity> listener){
        try {
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/currentuser/getCurrentUser";
            RequestManager.getInstance().post(url, HeaderUtils.getHeader(), null, listener);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 设置密码
     * @param listener
     */
    public void setPassword(String op, String np, DataCallback<Boolean> listener){
        try {
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/currentuser/setPassword/" + op + "/" + np;
            RequestManager.getInstance().post(url, HeaderUtils.getHeader(), null, listener);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}